package com.module.netty.util;

import com.alibaba.fastjson.JSONObject;
import com.module.common.utils.HexUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;

@Slf4j
public class NettyUtil {
    /**
     * bytebuf转string
     * @param msg
     * @return
     */
    public static String hexByteStr(ByteBuf msg) {
        String HEXES = "0123456789ABCDEF";
        byte[] req = new byte[msg.readableBytes()];
        msg.readBytes(req);
        final StringBuilder hex = new StringBuilder(2 * req.length);

        for (int i = 0; i < req.length; i++) {
            byte b = req[i];
            if(i>0){
                hex.append(" ");
            }
            hex.append(HEXES.charAt((b & 0xF0) >> 4))
                    .append(HEXES.charAt((b & 0x0F)));
        }
        return new String(hex);
    }

    /**
     * 根据连接对象获取ip
     * @param ctx
     * @return
     */
    public static InetSocketAddress getRemoteAddress(ChannelHandlerContext ctx){
        InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
        return insocket;
    }

    /**
     * 格式化上线帧
     * @param msg
     * @return
     */
    public static JSONObject parseRegister(String msg) {
        if(msg.startsWith("42424545")) {
            msg = msg.replace("42424545", "");
        } else {
            msg = msg.replace("BBEE", "");
        }
        String[] contents = HexUtils.hexStr2Str(msg).split(",");
        JSONObject jsonObject = new JSONObject();
        for(String content : contents) {
            if(content.contains("csq")) {
                int start = content.indexOf(":");
                String deviceCsq = content.substring(start + 1);
                jsonObject.put("deviceCsq", deviceCsq);
            } else if(content.contains("imei")) {
                int start = content.indexOf(":");
                String deviceImei = content.substring(start + 1);
                jsonObject.put("deviceImei", deviceImei);
            } else if(content.contains("iccid")) {
                int start = content.indexOf(":");
                String deviceIccid = content.substring(start + 1);
                jsonObject.put("deviceIccid", deviceIccid);
            } else if(content.contains("ver")) {
                int start = content.indexOf(":");
                String deviceVer = content.substring(start + 1, content.indexOf("}"));
                jsonObject.put("deviceVer", deviceVer);
            }
        }
        return jsonObject;
    }

    /**
     * DTU定时上报解析
     * @param msg
     * @return
     */
    public static JSONObject parseUpload(String msg) {
        String[] msgs = HexUtils.strChange(msg, 2);
        int start = 4;
        String deviceImei = HexUtils.strChangeStrByLength(msgs, start, 8, false);
        start += 8;
        String deviceImsi = HexUtils.strChangeStrByLength(msgs, start,8, false);
        start += 8;
        String deviceCsq = msgs[start];
        start += 1;
        String version = HexUtils.strChangeStrByLength(msgs, start, 5, false);
        String[] vers = HexUtils.strChange(version.substring(1), 3);
        String deviceVer = "v" + vers[0] + "." + vers[1] + "." + vers[2];

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceImei", deviceImei);
        jsonObject.put("deviceImsi", deviceImsi);
        jsonObject.put("deviceCsq", deviceCsq);
        jsonObject.put("deviceVer", deviceVer);
        return jsonObject;
    }
}
